from rich import print
from alive_progress import alive_bar

from util.load_puzzles import load_from_file
from visualizer.board_visualizer import visualize_board
from visualizer.comp import visualize_transforms, visualize_comp, visualize_components
import visualizer.common as v
from util.prechecker import *
from visualizer.precheck_visualizer import *
from puzzle.board import Board
from matplotlib.patches import Polygon
from puzzle.mathF import get_center

if __name__ == "__main__":
    board, _ = load_from_file("Final Design", "./data/des_final.txt", validation = False)
    _, comps = load_from_file("", "./data/des2_first.txt")
    
    if True:
        visualize_board(board)
        # visualize_precheck_result(board, precheck_1t(board))
        # visualize_precheck_result(board, precheck_2t(board))
        # visualize_precheck_result(board, precheck_3t(board))
        v.show()
    
    if False:
        tds = []
        pts = []
        for lb in ["Jul", "Aug", "31", "Sep", "Oct", "Sun"]:
            td = board.get_td_by_label(lb)
            tds.append(td)
            
            pts.append(
                (td.v_alone.x, td.v_alone.y)
            )
            pts.append(
                (td.v_left.x, td.v_left.y)
            )
            pts.append(
                (td.v_right.x, td.v_right.y)
            )
            
        
        fig, ax = plt.subplots()
        fig.canvas.manager.set_window_title(board.name) # type: ignore
        
        v.prepare_ax(ax)
        
        for td in tds:
            v.draw_polygon(
                ax,
                td.vertices,
                facecolor = (1, 0, 0)
            )
        
        hull = convex_hull(pts)
        
        v.draw_polygon(
            ax,
            hull,
            edgecolor = (0,0,1),
            facecolor = None,
            linewidth = 2.5
        )
        
        ax.xaxis.set_major_locator(NullLocator())
        ax.yaxis.set_major_locator(NullLocator())
        plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0) # https://blog.csdn.net/jifaley/article/details/79687000
        plt.margins(0, 0)
        
        # fig.savefig(board.name, dpi = 300, transparent = True, pad_inches = 0)
        
        v.show()
    
    # if True:
    #     for transforms in comps:
    #         visualize_transforms(transforms)
    #         v.show()
    
    if False:
        # cnt = 0
        # for c in sorted(comps, key = lambda x: len(x[0].td)):
        #     cnt += 1
        #     visualize_comp(c[0], str(cnt))
        # v.show()

        visualize_components(
            sorted(comps, key = lambda x: len(x[0].td))
        )
        v.show()
    
    if False:
        _, t = load_from_file("", "./data/comp.txt")
        for transforms in t:
            visualize_transforms(transforms)
            v.show()